var pagecount = 0;
var count = 0;
var pageIndex = 1;
var shopId = "shopId";
function Loadtable(json, params, tableId, DetailID) {
	var phArray = new Array();
	var count = 0;
	var DetailIDCount = 0;
	if (DetailID == null) {
		DetailID = "";
	}
	var DetailIDArr = new Array(DetailID.length);
	$(DetailID).each(function(index, ffData) {
		$.each(json.result.head, function(ffIndex, fffData) {
			if (fffData == ffData) {
				DetailIDArr[DetailIDCount++] = ffIndex;
			}
		});
	});

	if (params != null) {
		$(params).each(function(pIndex, pData) {
			$.each(json.result.head, function(hIndex, hData) {
				if (pData == hData) {
					phArray[count++] = hIndex;
				}
			});
		});
	}
	if (json.result.data != null) {
		$.each(json.result.data, function(index, dt) {
			$("table[id='" + tableId + "']").append("<tr></tr>");
			$(phArray).each(function(phIndex, data) {
				$.each($(dt), function(dtIndex, d) {
					if (dtIndex == data) {
						if (isHiddenFile(DetailIDArr, data)) {
							$("table[id='" + tableId + "'] tr:last").append("<td><input type='button' name='" + d + "' class='common_btn' onclick='popPoint(this.name)' value='立即验证'></td>");
						} else {
							$("table[id='" + tableId + "'] tr:last").append("<td>" + (d === null || d == "null" ? " " : d) + "</td>");
						}
					}
				});
			});
		});
	}
}

$(function() {
	refreshInput($(".serWrapper"));
	$("#research").bind('click', function() {

		var shopId = $("#shopName").val();
		//得到了店铺的id
		var custId = $("#custId").val().replace(/\s/gi, '');
		var mp = $("#mp").val().replace(/\s/gi, '');
		if (IsNotEmpty(mp)) {
			var exp = /(13|15|18)[0-9]{9}$/;
			if (!exp.test(mp)) {
				art.dialog({
					fixed : true,
					lock : true,
					content : '手机格式错误',
					icon : 'error',
					ok : true
				});
				$("#mp").focus();
				return false;
			}
		}
		var params = "memberCard=" + custId + "&mp=" + mp;
		if ($("#custId").val() != "" || $("#mp").val() != "") {
			likeQuery(params);
		} else {
			art.dialog({
				fixed : true,
				lock : true,
				content : "请输入查询条件",
				icon : 'error',
				ok : true
			});

		}
	});
});
function likeQuery(params) {
	//var url = "../member/find/shopId/"+shopId+"/custId/"+custId+"/mp/"+mp+"/0/10";
	$.ajax({
		url : "../member/find/checkCustomer",
		data : params,
		success : function(gbRst) {
			var rst = JSON.parse(gbRst);
			if (rst.state == 0) {
				if (rst.result.len == 0) {
					$("#Mp").val("");
					$("#custName").val("");
					$("#Tel").val("");
					$("#LevelName").val("");
					$("#ValidDateTime").val("");
					$("#Status").val("");
					$("#memberList tbody tr").remove();
					$("#sup").html("共" + 0 + "条");
					$("#pager").pager({
						pagenumber : pageIndex,
						pagecount : 0,
						buttonClickCallback : PageClick
					});

					art.dialog({
						lock : true,
						content : "查无此用户！",
						icon : 'error',
						ok : true
					});
				} else {
					$("#Mp").val(getValueByHeadOneRowGbRst(rst, "Mp"));
					$("#custName").val(getValueByHeadOneRowGbRst(rst, "custName"));
					$("#Tel").val(getValueByHeadOneRowGbRst(rst, "Tel"));
					$("#LevelName").val(getValueByHeadOneRowGbRst(rst, "levelName"));
					$("#ValidDateTime").val(getValueByHeadOneRowGbRst(rst, "ValidDateTime"));
					$("#Status").val(getValueByHeadOneRowGbRst(rst, "staName"));

					var memberId = getValueByHeadOneRowGbRst(rst, "ID");
					$("#memberId").val(memberId);
					queryService(memberId);
				}

			} else if (rst.state == 1) {
				art.dialog({
					fixed : true,
					lock : true,
					content : rst.msg,
					icon : 'error',
					ok : true
				});
			} else {
				checkStateShowErrorMsgAndDealResult(gbRst);
			}
		}
	});

}

function queryService(memberId) {
	// 	memberId=1286;
	var shopId = $("#shopName").val();
	//得到了店铺的id
	//       shopId=280000022;
	$("#memberList tbody tr").remove();
	var pi = pageIndex - 1;
	var params = "";
	if ($("#shopIsChoosed").is(":checked")) {
		if (shopId != "" && shopId != null) {
			params += "?shopId=" + shopId;
		}
	}
	var params = {'memberId':memberId}
	var url = " ../memberExclusiveService/find/memberOwnServiceList/" + pi + "/10";
	//      shopId=280000022;
	$.get(url,params, function(gbRst) {
		var rst = JSON.parse(gbRst);
		initTable(rst);
		if (rst.state == 0) {
			if (rst.result.len > 0) {
				var shu = rst.result.len;
				pagecount = Math.ceil(shu / 10);
			} else {
				pagecount = 0;

			}
			$("#sup").html("共" + rst.result.len + "条");
			$("#pager").pager({
				pagenumber : pageIndex,
				pagecount : pagecount,
				buttonClickCallback : PageClick
			});

		} else if (rst.state == 1) {
			art.dialog({
				fixed : true,
				lock : true,
				content : rst.msg,
				icon : 'error',
				ok : true
			});

		} else {
			checkStateShowErrorMsgAndDealResult(gbRst);
		}
	});

}

PageClick = function(pageclickednumber) {
	pageIndex = pageclickednumber;
	var memberId = $("#memberId").val().replace(/\s/gi, '');
	queryService(memberId);
};
function initTable(json) {
	if (json != null && json.state == 0) {
		/*var params = new Array("name", "typeName", "nums", "useNum", "createFileTime", "EffectDate", "UneffectDate", "staName", "DetailID");
		// "PayCash", "PayPos", "PayDepstAmt", "PayCheck", "PayPersonCard", "PayIntAmtMny", "StaName", "PaidName"
		// 		var url = "html/carRepair/jsp/receiptCarHistoryDetail.jsp";
		var DetailID = new Array("DetailID");
		Loadtable(json, params, "memberList", DetailID);*/
		for(var i=0;i<json.result.data.length;i++){
			
			var htm="<tr>"
				htm+="<td>"+_getStringOfJson(i, json, "name")+"</td>";
			htm+="<td>"+_getStringOfJson(i, json, "typeName")+"</td>";
			htm+="<td>"+_getStringOfJson(i, json, "nums")+"</td>";
			htm+="<td>"+_getStringOfJson(i, json, "useNums")+"</td>";
			htm+="<td>"+_getStringOfJson(i, json, "creatimeTime")+"</td>";
			htm+="<td>"+_getStringOfJson(i, json, "UneffectDate")+"</td>";
			var state = _getIntOfJson(i, json, "status");
			var nums = _getIntOfJson(i, json, "nums");
			var useNum = _getIntOfJson(i, json, "useNums");
			if(nums!=0){
				if(nums<=useNum){
					htm+="<td>服务次数已用完</td>";
					htm+="<td></td>";
				}else{
					if(state==1){
						htm+="<td>有效</td>";
						htm+="<td><input class=\"common_btn\" type=\"button\" value=\"使用\" onclick=\"popPoint("+_getIntOfJson(i, json, "id")+")\"></td>";

					}else{
						htm+="<td>失效</td>";
						htm+="<td></td>";
					}
				}
				
			}else{
				if(state==1){
					htm+="<td>有效</td>";
					htm+="<td><input class=\"common_btn\" type=\"button\" value=\"使用\" onclick=\"popPoint("+_getIntOfJson(i, json, "id")+")\"></td>";

				}else{
					htm+="<td>失效</td>";
					htm+="<td></td>";

				}
			}
			
				htm+="</tr>"
			$("#memberList tbody").append(htm);		
		}
		
		
		
		
		
	}
}

function downRescouse() {
	var memberId = $("#memberId").val().replace(/\s/gi, '');
	// 	memberId=1286;
	var shopId = $("#shopName").val().replace(/\s/gi, '');
	//得到了店铺的id
	var param = "memberId=" + memberId + "&shopId=" + shopId + "&downType=10";
	window.location.href = "../csvdown/down/csv/down?" + param;
}

function checkCoupons(id) {
	var url = " ../memberExclusiveService/check";

	$.post(url, {
		"id" : id
	}, function(gbRst) {
		var rst = JSON.parse(gbRst);
		if(rst.state!=0){
			alertErrorMessage(rst.msg)
			return false;
		}
		
		var shopId = $("#shopName").val();
		//得到了店铺的id
		var custId = $("#custId").val().replace(/\s/gi, '');
		var mp = $("#mp").val().replace(/\s/gi, '');
		var params = "memberCard=" + custId + "&mp=" + mp;
		likeQuery(params);

		var response = new qmsk.Response(rst);
		var consumeId = response.getValue("consumeId");
		art.dialog({
			fixed : true,
			lock : true,
			content : "操作成功，是否打印凭证？",
			icon : 'question',
			ok : function() {
				print(consumeId);
			},
			cancel : true
		});
	
	});

}

function print(consumeId) {
	window.open("printMemberServiceConsume.html?id=" + consumeId);
}

function popPoint(id) {
	// 	onclick='checkCoupons(name='"+d+"')'
	art.dialog({
		fixed : true,
		lock : true,
		content : "确认使用该服务？",
		icon : 'question',
		ok : function() {
			checkCoupons(id);
		},
		cancel : true
	});
}